home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / promptlib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  7.3 KB  |  260 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. _pre_functions = []
  5. _post_functions = []
  6.  
  7. def register_pre_handler(pred, pre):
  8.     _pre_functions.insert(0, (pred, pre))
  9.  
  10.  
  11. def register_post_handler(pred, post):
  12.     _post_functions.insert(0, (pred, post))
  13.  
  14.  
  15. def register_prompt_handler(f):
  16.     funcs = f()
  17.     pred = funcs.get('pred')
  18.     pre = funcs.get('pre')
  19.     post = funcs.get('post')
  20.     if pred is None:
  21.         raise Exception("%r doesn't seem to be the right sort of function for a prompt handler", f)
  22.     
  23.     if pre is not None:
  24.         register_pre_handler(pred, pre)
  25.     
  26.     if post is not None:
  27.         register_post_handler(pred, post)
  28.     
  29.     return f
  30.  
  31.  
  32. def bool_type():
  33.     
  34.     def pred(o, d):
  35.         return o is bool
  36.  
  37.     
  38.     def pre(o, d):
  39.         if d is True:
  40.             return ('', '(Y/n)')
  41.         elif d is False:
  42.             return ('', '(y/N)')
  43.         else:
  44.             return ('', '(y/n)')
  45.  
  46.     
  47.     def post(t, o, d):
  48.         t = t.lower()
  49.         if t in ('y', 'n'):
  50.             return None if t == 'y' else False
  51.         
  52.         return d
  53.  
  54.     return locals()
  55.  
  56. bool_type = register_prompt_handler(bool_type)
  57.  
  58. def confirm_type():
  59.     
  60.     def pred(o, d):
  61.         if o == 'confirm':
  62.             pass
  63.         return d is not None
  64.  
  65.     
  66.     def pre(o, d):
  67.         return ('', ': type "%s" (or "CANCEL" to cancel)' % d)
  68.  
  69.     
  70.     def post(t, o, d):
  71.         if t == d:
  72.             return True
  73.         elif t == 'CANCEL':
  74.             return False
  75.         else:
  76.             return None
  77.  
  78.     return locals()
  79.  
  80. confirm_type = register_prompt_handler(confirm_type)
  81.  
  82. def str_type():
  83.     
  84.     def pred(o, d):
  85.         return o is str
  86.  
  87.     
  88.     def pre(o, d):
  89.         return ('(default = %r)' % (d,), '')
  90.  
  91.     
  92.     def post(t, o, d):
  93.         if not t:
  94.             pass
  95.         return d
  96.  
  97.     return locals()
  98.  
  99. str_type = register_prompt_handler(str_type)
  100.  
  101. def strlist_type():
  102.     
  103.     def pred(o, d):
  104.         return o is list
  105.  
  106.     
  107.     def pre(o, d):
  108.         return ('(default = %r)' % (d,), '(comma-separated)')
  109.  
  110.     
  111.     def post(t, o, d):
  112.         if t:
  113.             return map(str.strip, t.strip(',').split(','))
  114.         else:
  115.             return d
  116.  
  117.     return locals()
  118.  
  119. strlist_type = register_prompt_handler(strlist_type)
  120.  
  121. def list_type():
  122.     
  123.     def pred(o, d):
  124.         return type(o) is list
  125.  
  126.     
  127.     def pre(o, d):
  128.         options_str = '\n\t' + '\n\t'.join((lambda .0: for i, s in .0:
  129. '(%d) %s' % (i + 1, s))(enumerate(o)))
  130.         default_str = '\n(default = %r)' % d
  131.         return (default_str, options_str)
  132.  
  133.     
  134.     def post(t, o, d):
  135.         if not t:
  136.             return d
  137.         
  138.         
  139.         try:
  140.             idx = int(t)
  141.         except Exception:
  142.             return None
  143.  
  144.         if idx == 0:
  145.             return None
  146.         
  147.         
  148.         try:
  149.             return o[idx - 1]
  150.         except IndexError:
  151.             return None
  152.  
  153.  
  154.     return locals()
  155.  
  156. list_type = register_prompt_handler(list_type)
  157.  
  158. def pre_prompt(prompt_str, options, default):
  159.     pre_func = find_pre_function(options, default)
  160.     if pre_func is None:
  161.         raise NotImplementedError("Don't know what to do for options = %r, default = %r", options, default)
  162.     
  163.     (default_str, options_str) = pre_func(options, default)
  164.     full_prompt_str = ('%s %s %s' % (prompt_str, options_str, default_str)).strip() + ': '
  165.     return (full_prompt_str, options, default)
  166.  
  167.  
  168. def prompt(prompt_str, options = None, default = None, input_func = raw_input):
  169.     (prompt_str, options, default) = pre_prompt(prompt_str, options, default)
  170.     result = None
  171.     while result is None:
  172.         
  173.         try:
  174.             text = input_func(prompt_str).strip()
  175.         except Exception:
  176.             e = None
  177.             raise e
  178.             continue
  179.  
  180.         result = post_prompt(text, options, default)
  181.     return result
  182.  
  183.  
  184. def find_match_for(pred_func_list, *args):
  185.     for pred, func in pred_func_list:
  186.         if pred(*args):
  187.             return func
  188.             continue
  189.     
  190.  
  191.  
  192. def find_pre_function(options, default):
  193.     return find_match_for(_pre_functions, options, default)
  194.  
  195.  
  196. def find_post_function(options, default):
  197.     return find_match_for(_post_functions, options, default)
  198.  
  199.  
  200. def post_prompt(text, options, default):
  201.     post_func = find_post_function(options, default)
  202.     if post_func is None:
  203.         raise NotImplementedError("Don't know what to do for options = %r, default = %r", options, default)
  204.     
  205.     return post_func(text, options, default)
  206.  
  207.  
  208. def _main():
  209.     booltests = ((bool, True, [
  210.         'n'], False), (bool, False, [
  211.         'y'], True), (bool, None, [
  212.         '',
  213.         'y'], True), (bool, True, [
  214.         ''], True), (bool, False, [
  215.         ''], False))
  216.     confirmtests = (('confirm', 'b', [
  217.         'b'], True), ('confirm', 'b', [
  218.         'CANCEL'], False), ('confirm', 'b', [
  219.         'a',
  220.         'CANCEL'], False), ('confirm', 'b', [
  221.         'a',
  222.         'b'], True))
  223.     strtests = ((str, 'a', [
  224.         'b'], 'b'), (str, 'a', [
  225.         ''], 'a'), (str, None, [
  226.         'b'], 'b'), (str, None, [
  227.         '',
  228.         'a'], 'a'))
  229.     strlisttests = ((list('abcd'), 'a', [
  230.         ''], 'a'), (list('abcd'), 'a', [
  231.         ''], 'a'), (list('abcd'), 'a', [
  232.         ''], 'a'), (list('abcd'), 'a', [
  233.         ''], 'a'))
  234.     listtests = ()
  235.     for tests in (booltests, confirmtests, strtests, strlisttests, listtests):
  236.         for opts, default, inputs, expected in tests:
  237.             
  238.             def input():
  239.                 yield None
  240.                 for input in inputs:
  241.                     yield input
  242.                 
  243.                 while True:
  244.                     yield ''
  245.  
  246.             input_gen = input()
  247.             input_gen.next()
  248.             result = prompt(repr(opts), opts, default, input_gen.send)
  249.             input_gen.close()
  250.             if result == expected:
  251.                 print ' O'
  252.                 continue
  253.             print ' X (%r != %r)' % (result, expected)
  254.         
  255.     
  256.  
  257. if __name__ == '__main__':
  258.     _main()
  259.  
  260.